package demo01;

import Utils.JDBCUtils;
import demo02.MyConnection2;
import demo02.MyConnection3;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

/*
    自定义的数据库连接池
 */
public class MyDataSource implements DataSource {

    //准备容器,用于保存多个数据库连接对象
    private static List<Connection> pool= Collections.synchronizedList(new ArrayList<Connection>());
    //静态代码块,生成10个数据库连接保存到集合中
    static {
        for (int i = 0; i < 10; i++) {
           Connection con= JDBCUtils.getConnection();
           pool.add(con);
        }
    }
    //返回连接池的大小
    public int getSize(){
        return pool.size();
    }

    @Override
    public Connection getConnection() throws SQLException {
        if(pool.size()>0){
            //从池中获取数据库连接
            Connection con=pool.remove(0);
            Connection proxycon= (Connection) Proxy.newProxyInstance(con.getClass().getClassLoader(), new Class[]{Connection.class}, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getName().equals("close")) {
                        pool.add(con);
                        return null;
                    }else {
                        return method.invoke(con,args);
                    }
                }

            });
            return proxycon;
        }else {
            throw new RuntimeException("连接数量已用尽");
        }
    }

/*    //从池子中返回一个数据库连接
    @Override
    public Connection getConnection() throws SQLException {
        if(pool.size()>0){
            //从池中获取数据库连接
            Connection con=pool.remove(0);
//            MyConnection2 mycon = new MyConnection2(con, pool);
            MyConnection3 mycon=new MyConnection3(con,pool);
            return mycon;
        }else {
            throw new RuntimeException("连接数量已用尽");
        }
    }*/

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }
}
